home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / fsp-2.7 / fsp-2 / fsp / clients / fgrabcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-21  |  4.3 KB  |  192 lines

  1.     /*********************************************************************\
  2.     *  Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu)   *
  3.     *                                                                     *
  4.     *  You may copy or modify this file in any manner you wish, provided  *
  5.     *  that this notice is always included, and that you hold the author  *
  6.     *  harmless for any loss or damage resulting from the installation or *
  7.     *  use of this software.                                              *
  8.     \*********************************************************************/
  9.  
  10. #include "tweak.h"
  11. #include "client_def.h"
  12. #include "c_extern.h"
  13. #include "bsd_extern.h"
  14. #include <signal.h>
  15.  
  16. int grab_clobbertype=CLOBBERONFIND;
  17. char *fname;
  18. int optletter;
  19. int suffix;
  20. char *tname;
  21. unsigned long start_from;
  22. extern int optind;
  23.  
  24. static void fsp_cleanup PROTO1(int, signum)
  25. {
  26.   char filename[20];
  27. #ifndef VMS
  28.   sprintf(filename,".fsp.%d",getpid());
  29. #else /* no more than one dot allowed in filenames */
  30.   sprintf(filename,"fsp_%d",getpid());
  31. #endif
  32.   unlink(filename);
  33.   exit(1);
  34. }
  35.  
  36. static int grab_file PROTO1(char *, path)
  37. {
  38.   char *name, *t2;
  39. #ifdef VMS
  40.   char filename[255];
  41. #endif
  42.   FILE *fp;
  43.   struct stat statbuf;
  44.   
  45.   for(name = t2 = path; *t2; t2++)
  46.     if(*t2 == '/') name = t2 + 1;
  47.   
  48. #ifdef VMS /* convert more than one dot to underscores */
  49.   strcpy(filename,name); name=filename;
  50.   convdots(name, filename);
  51. #endif /* VMS */
  52.   
  53.   if (grab_clobbertype==NOCLOBBER) {
  54.     if (fp=fopen(name,"r")) {
  55.       fclose(fp);
  56.       fprintf(stderr,"Will not overwrite existing file %s\n",name);
  57.       return;
  58.     }
  59.   }
  60.   
  61.   if (grab_clobbertype==UNIQUE) {
  62.     fname=name;
  63.     name=(char *)malloc(strlen(fname)+5);
  64.     strcpy(name,fname);
  65.     for (suffix=0 ; (fp=fopen(name,"r")) ; suffix++) {
  66.       fclose(fp);
  67.       sprintf(name,"%s-%d",fname,suffix);
  68.     }
  69.   }
  70.   
  71.   if (grab_clobbertype==CLOBBERONFIND) {
  72.     fname=name;
  73.     name=(char*)malloc(20);
  74. #ifdef VMS /* no more than one dot allowed in filenames */
  75.     sprintf(name,"fsp_%d",getpid());
  76. #else
  77.     sprintf(name,".fsp.%d",getpid());
  78. #endif
  79.   }
  80.   
  81.   if(grab_clobbertype == APPEND) {
  82.     if(stat(name, &statbuf) == 0) {
  83.       start_from = statbuf.st_size;
  84.       if((fp = fopen(name,"a")) == NULL) perror(name);
  85.     } else start_from = -1;
  86.   } else start_from = -1;
  87.   
  88.   if(start_from == -1) {
  89.     fp = fopen(name, "w");
  90.     start_from = 0;
  91.   }
  92.   
  93.   if(fp) {
  94.     if(util_grab_file(path,fp,start_from) == -1) {
  95.       fclose(fp); unlink(name);
  96.     } else
  97.       fclose(fp);
  98.   } else fprintf(stderr,"Cannot write %s\n",name);
  99.   
  100.   if (grab_clobbertype==CLOBBERONFIND) {
  101.     rename(name,fname);
  102.     free(name);
  103.   }
  104.   
  105.   if (grab_clobbertype==UNIQUE) {
  106.     free(name);
  107.   }
  108. }
  109.  
  110. int main PROTO3(int, argc, char **, argv, char **, envp)
  111. {
  112.   char **av, *av2[2], n[1024];
  113.   int prompt;
  114.   
  115.   signal(SIGHUP,fsp_cleanup);
  116.   signal(SIGINT,fsp_cleanup);
  117.   signal(SIGQUIT,fsp_cleanup);
  118.   signal(SIGILL,fsp_cleanup);
  119.   signal(SIGTRAP,fsp_cleanup);
  120.   signal(SIGFPE,fsp_cleanup);
  121. #ifndef __linux__
  122.   signal(SIGBUS,fsp_cleanup);
  123.   signal(SIGEMT,fsp_cleanup);
  124.   signal(SIGSYS,fsp_cleanup);
  125. #endif
  126.   signal(SIGSEGV,fsp_cleanup);
  127.   signal(SIGPIPE,fsp_cleanup);
  128.   signal(SIGTERM,fsp_cleanup);
  129.   
  130.   env_client();
  131.   if (strcmp(env_local_dir,".") && chdir(env_local_dir)) {
  132.     perror("chdir");
  133.     exit(1);
  134.   }
  135.   
  136.   /* Parse options
  137.    * -f forces overwrite
  138.    * -u forces unique names
  139.    * -t uses temporary file to download
  140.    * -n forces noclobber
  141.    * -a append to files if they exist
  142.    */
  143.   while ((optletter=getopt(argc, argv,"futna")) != EOF)
  144.     switch (optletter) {
  145.       case 'f':
  146.         grab_clobbertype=CLOBBER;
  147.     break;
  148.       case 'u':
  149.     grab_clobbertype=UNIQUE;
  150.     break;
  151.       case 't':
  152.     grab_clobbertype=CLOBBERONFIND;
  153.     break;
  154.       case 'n':
  155.     grab_clobbertype=NOCLOBBER;
  156.     break;
  157.       case 'a':
  158.     grab_clobbertype = APPEND;
  159.     }
  160.   
  161.   if(argc > optind) {
  162.     for(; argc > optind; optind++) {
  163.       if(!(av = glob(argv[optind]))) {
  164.     av = av2;
  165.     av2[0] = argv[optind];
  166.     av2[1] = 0;
  167.       }
  168.       while(*av) grab_file(*av++);
  169.     }
  170.   } else {
  171.     prompt = isatty(0);
  172.     while(1) {
  173.       if(prompt) {
  174.     fputs("fgrab: ",stdout);
  175.     fflush(stdout);
  176.       }
  177.       if(!gets(n)) break;
  178.       if(!*n) continue;
  179.       if(!(av = glob(n))) {
  180.     av = av2;
  181.     av2[0] = n;
  182.     av2[1] = 0;
  183.       }
  184.       while(*av) grab_file(*av++);
  185.     }
  186.   }
  187.   
  188.   client_done();
  189.   
  190.   exit(0);
  191. }
  192.